/*
Learning from Law Enforcement
Libor Dusek and Christian Traxler

Do-file producing regression results and figures in the paper and (online) appendix

*************************************
*** DESCRIPTION AND PRELIMINARIES
**************************************

*** This do file: RD_06.do
    RD Analyses using supplementary data from new speed cameras as well as from the post reform period at the old cameras
    => I.  RD Estimates at high-fine cutoff - supplementary data (Tab. A.6)
    => II. Long run effects at new radards, Enforcement Cutoff - suppl.data (Tab C.2)

*** Required packages:
    rdrobust

*** The input dataset:
    Dusek_Traxler_RD_supplementary.dta
	
*** Output is stored in output directories: OUTPUT/RD_TAB  OUTPUT/RD_FIG , and OUTPUT/RD_temp
	Names of outpupt files correspond to the Table/Figure numbers in the paper and (online) appendix
	
*** List of Figures and Tables produced:
    => Table A.6 and Table C.2
    See also Section C of the ReadMe File.
*/

cap clear
set more off

/* Set directories */
global dir C:/yourdir/
cd    $dir
global datadir $dir/datasets/
global outputdir outfiles/
capture mkdir ${outputdir}
capture mkdir ${outputdir}/RD_TAB
capture mkdir ${outputdir}/RD_FIG
capture mkdir ${outputdir}/RD_temp

cap log close
log using    $dir/${outputdir}/log_RD_06.txt, replace

/* Load Supplementary Data (defined for A = F = 4) */
use $datadir/Dusek_Traxler_RD_supplementary.dta, clear

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* I.    RD ESTIMATES AT HIGH-FINE CUTOFF - SUPPLEMENTARY DATA  => Tab. A.6
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.A   DEFINE SAMPLE AND DERIVES VARIABLES
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Defines assignment speed */
gen speed_Z = maxspeed - 23

/* focus on sample around Cutoff 2: maxspeed above Cutoff 1 (14km/h above
   limit) and below the 3rd cutoff (43km/h above the limit) */
keep if speed_Z>-9 & speed_Z<20

/* keep only "relevant" observations: pre & post period */
keep if (period_pre==1 | period_post==1 | dum_trigger==1)

/* Cut Data at End of sample period */
keep if day_trigger<td(1feb2020)

/* Indicator for new data, new/old speed cameras */
gen     new_data = 0 if day_trigger<td(1jul2017)
replace new_data = 1 if day_trigger>=td(1jul2017)

gen     old_radar=0 if radarid>5
replace old_radar=1 if radarid<=5

/* define first relevant "episode" (after sample restrictions) */
gsort id episode
gegen epi_min=min(episode), by(id)

/* Identifier for first relevant drive through  */
gegen xxid_record_pre_min  = min(id_record) if (period_pre==1 | dum_trigger==1), by(id episode)
gegen id_record_pre_min  = mean(xxid_record_pre_min), by(id episode)
drop xxid_record_*

/*High Fine Ticket Indicator */
gen     xx_highfine=0
replace xx_highfine=1 if dum_trigger==1 & violcat2==1
gegen high_fine = max(xx_highfine), by(id episode)
gen dum_ticketed_high     = dum_ticketed     * high_fine

/*TREATMENT RATE (2): share of rides after receiving HIGH FINE ticket */
gegen xxticketed_m = mean(dum_ticketed_high) if period_post==1, by(id episode)
gegen treated_high_m = mean(xxticketed_m), by(id episode)
drop xx_* xxticketed*

/*SPEEDING RATE */
gegen xxspeeding_m     = mean(speeding) if period_post==1, by(id episode)
gegen speeding_m     = mean(xxspeeding_m), by(id episode)
drop xxspeeding_*

/*Re-OFFENSE RATE*/
gegen xxviol_m     = mean(violation) if period_post==1, by(id episode)
gegen violation_m     = mean(xxviol_m), by(id episode)
drop xxviol_*

/* MEAN SPEED */
gegen xxspeedm_m    = mean(speedm) if period_post==1, by(id episode)
gegen speedm_m    = mean(xxspeedm_m), by(id episode)
drop xxspeedm_*

/*SPEED PERCENTILES */
gegen xxspeedm_p50  = median(speedm) if period_post==1, by(id episode)
gegen speedm_p50  = mean(xxspeedm_p50), by(id episode)

gegen xxspeedm_p90  = pctile(speedm) if period_post==1, p(90) by(id episode)
gegen speedm_p90  = mean(xxspeedm_p90), by(id episode)
drop xxspeedm_*


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.B   Wald Estimates at High-Fine Cutoff, at car level
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

gen     ESTSAMPLE=0
replace ESTSAMPLE=1 if id_record==id_record_pre_min & episode==epi_min & dum_haspost==1

/* Table A6 - Panel A: old speed cameras but new data */

rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & old_radar==1 & new_data==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1  & old_radar==1 & new_data==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) replace

foreach Y of varlist  violation_m  speedm_m  speedm_p50 speedm_p90 {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1  & old_radar==1 & new_data==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  local bw = h_mserd
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1  & old_radar==1 & new_data==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* Table A6 - Panel B: old speed cameras, all data */

rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & old_radar==1 , fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1  & old_radar==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) replace

foreach Y of varlist  violation_m  speedm_m  speedm_p50  speedm_p90 {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1  & old_radar==1 , fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  local bw = h_mserd
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1  & old_radar==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/*  Table A6 - Panel C : new speed cameras, new data */

rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & old_radar==0 & new_data==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1  & old_radar==0 & new_data==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) replace

foreach Y of varlist  violation_m  speedm_m  speedm_p50  speedm_p90 {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1  & old_radar==0 & new_data==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  local bw = h_mserd
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1  & old_radar==0 & new_data==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* Tab A6 - Panel D : all data */

rdrobust speeding_m  speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) replace

foreach Y of varlist  violation_m speedm_m  speedm_p50  speedm_p90 {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1, fuzzy(treated_high_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  local bw = h_mserd
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.C   Wald Estimates at High-Fine Cutoff, at RIDE level
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

replace ESTSAMPLE=0
replace ESTSAMPLE=1 if episode==epi_min & dum_haspost==1 & period_post==1

/* Table A6 - Panel A: => appends ride level estimates (2 columsn ) */

rdrobust speeding speed_Z if ESTSAMPLE==1 & old_radar==1 & new_data==1, fuzzy(dum_ticketed_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & old_radar==1 & new_data==1
  local y_l = `r(mean)'
  local rel = 100* e(tau_bc)/`r(mean)'
 outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) append

foreach Y of varlist speedm {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1 & old_radar==1 & new_data==1, fuzzy(dum_ticketed_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
  local bw = h_mserd

  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & old_radar==1 & new_data==1
  local y_l = `r(mean)'
  local rel = 100*  e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* Table A6 - Panel B: => appends ride level estimates (2 columsn ) */

rdrobust speeding speed_Z if ESTSAMPLE==1 & old_radar==1, fuzzy(dum_ticketed_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & old_radar==1
  local y_l = `r(mean)'
  local rel = 100* e(tau_bc)/`r(mean)'
 outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) append

foreach Y of varlist speedm {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1 & old_radar==1 , fuzzy(dum_ticketed_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
  local bw = h_mserd

  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & old_radar==1
  local y_l = `r(mean)'
  local rel = 100*  e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* Table A6 - Panel C: => appends ride level estimates (2 columsn ) */

rdrobust speeding speed_Z if ESTSAMPLE==1 & old_radar==0 & new_data==1, fuzzy(dum_ticketed_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & old_radar==0 & new_data==1
  local y_l = `r(mean)'
  local rel = 100* e(tau_bc)/`r(mean)'
 outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) append

foreach Y of varlist speedm {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1 & old_radar==0 & new_data==1, fuzzy(dum_ticketed_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
  local bw = h_mserd

  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & old_radar==0 & new_data==1
  local y_l = `r(mean)'
  local rel = 100*  e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-C.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* Table A6 - Panel D: => appends ride level estimates (2 columsn ) */

rdrobust speeding speed_Z if ESTSAMPLE==1, fuzzy(dum_ticketed_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = `r(mean)'
  local rel = 100* e(tau_bc)/`r(mean)'
 outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) append

foreach Y of varlist speedm {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1, fuzzy(dum_ticketed_high) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
  local bw = h_mserd

  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = `r(mean)'
  local rel = 100*  e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A6_Panel-D.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* II.   "LONG RUN" RD ESTIMATES (Enforcement Cutoff) - SUPPL.DATA  => Tab C.2
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/



cap clear
use $datadir/Dusek_Traxler_RD_supplementary.dta, clear

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* II.A   DEFINE SAMPLE AND DERIVES VARIABLES
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

* Defines assignment speed, now at enforcement cutoff */
gen speed_Z = maxspeed - 14


/* Speed Camera Specific "Start Day" */
gegen radar_start_day=min(day), by(radarid)

/* Episode specific identifier for first entry*/
gegen xxid_record_pre_min  = min(id_record) if (period_pre==1 | dum_trigger==1), by(id episode)
gegen id_record_pre_min  = mean(xxid_record_pre_min), by(id episode)
drop xxid_record_*

/* Define Assingment Speed ("speed_Z_main") from the inital five cameras */
gen speed_Z_temp=speed_Z if day_trigger<td(20jul2017)
gegen xxspeed_Z_main=max(speed_Z_temp), by(id)
gegen speed_Z_main=max(xxspeed_Z_main), by(id)

/* Identify episode where this assingment speed was reached */
gen     max_epi=0
replace max_epi=1 if speed_Z == speed_Z_main
gegen xxepi_relevant=mean(episode) if max_epi==1, by(id)
gegen epi_relevant=mean(xxepi_relevant), by(id)
drop xxspeed_* xxepi_*

/* Drop earlier episodes */
drop if episode < epi_relevant

/* Define Estimation Sample: only includes assignment speed generated before at
   initial five speed cameras  */
gen     ESTSAMPLE=0
replace ESTSAMPLE=1 if id_record==id_record_pre_min & max_epi==1 & day_trigger<td(20jul2017)

/* Reduce Data in Memory, I */
drop if speed_Z<-14 & ESTSAMPLE==1
drop if speed_Z>9   & ESTSAMPLE==1

/* Reduce Data in Memory, II */
gegen relevant_car = max(ESTSAMPLE), by(id)
keep if relevant_car==1
drop relevant_car

/* Reduce Data in Memory, III */
drop if radarid<=5 & ESTSAMPLE == 0
drop if radarid<=5 & episode > epi_relevant

/* Speed */
gen relspeedm= speedm - speedlimit

/* Time Window (month) for Outcome Measurement */
local f = 12

/* Defines Outcomes at the new speed cameras ( f- month after start) */

/*SPEEDING RATE*/
 qui gegen xxspeeding_m_new_`f'     = mean(speeding) if radarid>5 & day > radar_start_day  & day<=(radar_start_day + `f'*30), by(id)
 qui gegen speeding_m_new_`f'     = mean(xxspeeding_m_new_`f'), by(id)
 qui drop xxspeeding_*
/*SPEED  (+normalized wrt speed limit) */
 qui gegen xxspeedm_m_new_`f'    = mean(relspeedm) if radarid>5 & day > radar_start_day  & day<=(radar_start_day + `f'*30), by(id)
 qui gegen speedm_m_new_`f'    = mean(xxspeedm_m_new_`f'), by(id)
 qui gegen xxspeedm_m_nonnormal_`f'    = mean(speedm) if radarid>5 & day > radar_start_day  & day<=(radar_start_day + `f'*30), by(id)
 qui gegen speedm_m_nonnormal_`f'    = mean(xxspeedm_m_nonnormal_`f'), by(id)
 qui drop xxspeedm_*
/*SPEED - median (+normalized wrt speed limit) */
 qui gegen xxspeedm_p50_new_`f'    = pctile(relspeedm) if radarid>5 & day > radar_start_day  & day<=(radar_start_day + `f'*30), p(50) by(id)
 qui gegen speedm_p50_new_`f'    = mean(xxspeedm_p50_new_`f'), by(id)
 qui gegen xxspeedm_p50_nonnormal_`f'    = pctile(speedm) if radarid>5 & day > radar_start_day  & day<=(radar_start_day + `f'*30), p(50) by(id)
 qui gegen speedm_p50_nonnormal_`f'    = mean(xxspeedm_p50_nonnormal_`f'), by(id)
 qui drop xxspeedm*
/*SPEED - 90th percentiles (+normalized wrt speed limit) */
 qui gegen xxspeedm_p90_new_`f'    = pctile(relspeedm) if radarid>5 & day > radar_start_day  & day<=(radar_start_day + `f'*30), p(90) by(id)
 qui gegen speedm_p90_new_`f'    = mean(xxspeedm_p90_new_`f'), by(id)
 qui gegen xxspeedm_p90_nonnormal_`f'    = pctile(speedm) if radarid>5 & day > radar_start_day  & day<=(radar_start_day + `f'*30), p(90) by(id)
 qui gegen speedm_p90_nonnormal_`f'    = mean(xxspeedm_p90_nonnormal_`f'), by(id)
 qui drop xxspeedm*


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* II.B   Reduced Form Estimates, Car Level => Table C.2, Col.1--4
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust speeding_m_new_12 speed_Z_main if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
   local bw = h_mserd
   qui su speeding_m_new_12 if speed_Z_main<0 & speed_Z_main>-0.5 & ESTSAMPLE==1
   local y_l = r(mean)
   local rel = 100 * e(tau_bc)/`y_l'
   qui outreg2 using ${outputdir}/RD_TAB/Tab_C2_new_cameras.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(Speeding) replace

rdrobust speedm_m_new_12 speed_Z_main if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
   local bw = h_mserd
   qui su speedm_m_nonnormal_12 if speed_Z_main<0 & speed_Z_main>-0.5 & ESTSAMPLE==1
   local y_l = r(mean)
   local rel = 100 * e(tau_bc)/`y_l'
   qui outreg2 using ${outputdir}/RD_TAB/Tab_C2_new_cameras.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(Speed) append

rdrobust speedm_p50_new_12 speed_Z_main if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
   local bw = h_mserd
   qui su speedm_p50_nonnormal_12 if speed_Z_main<0 & speed_Z_main>-0.5 & ESTSAMPLE==1
   local y_l = r(mean)
   local rel = 100 * e(tau_bc)/`y_l'
   qui outreg2 using ${outputdir}/RD_TAB/Tab_C2_new_cameras.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(Speed-p50) append

rdrobust speedm_p90_new_12 speed_Z_main if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
   local bw = h_mserd
   qui su speedm_p90_nonnormal_12 if speed_Z_main<0 & speed_Z_main>-0.5 & ESTSAMPLE==1
   local y_l = r(mean)
   local rel = 100 * e(tau_bc)/`y_l'
   qui outreg2 using ${outputdir}/RD_TAB/Tab_C2_new_cameras.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(Speed-p50) append


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* II.C   Reduced Form Estimates, Ride Level => Table C.2, Col.5--6
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Dummies for Radard Fixed Effects: helpful to reduce variance in speed
   measures from (heterogeneous types/locations of) new cameras */
qui tab radarid,gen(radar_)
global controls radar_7 - radar_31


/* Time Window (month) for Outcome Measurement */
local f = 12

/* WARNING: >9mio observations, computationally intense step, may last 1-2h */
foreach Y of varlist speeding speedm  {
  rdrobust `Y' speed_Z_main                        if radarid > 5 & day > radar_start_day  & day<=(radar_start_day + `f'*30) , all p(1) bwselect(mserd) vce(cluster id) covs($controls) kernel(tri)
   local bw = h_mserd
   qui su `Y' if speed_Z_main<0 & speed_Z_main>-0.5 & radarid > 5 & day > radar_start_day  & day<=(radar_start_day + `f'*30)
   local y_l = r(mean)
   local rel = 100 * e(tau_bc)/`y_l'
  qui outreg2 using ${outputdir}/RD_TAB/Tab_C2_new_cameras.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(`Y') append
}

log close
